package org.dendy.browser.menubar;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.dendy.browser.ManagerBrowser;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.printing.PrintDialog;
import org.eclipse.swt.printing.Printer;
import org.eclipse.swt.printing.PrinterData;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

/**
 * Class ini berfungsi untuk menu bar hanya berisi File dari browser
 * 
 * @author dendy
 * 
 */
public class MenuBarFile extends MenuBarBrowser {

	public MenuBarFile(Shell shell) {
		this.shell = shell;
		bar = new Menu(shell, SWT.BAR);
	}

	@Override
	public Menu createMenuItem() {
		bar = shell.getMenuBar();
		Menu menu = new Menu(bar);

		// File->Save
		item = new MenuItem(menu, SWT.PUSH);
		item.setText("Save Page As..\t\t Ctrl+S");
		item.setAccelerator(SWT.MOD1 + 'S');
		item.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				saveFile();
			}
		});

		item = new MenuItem(menu, SWT.SEPARATOR);

		// File->Print
		item = new MenuItem(menu, SWT.PUSH);
		item.setText("Print..\t\t Ctrl+P");
		item.setAccelerator(SWT.MOD1 + 'P');
		item.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				printFile();
			}
		});

		item = new MenuItem(menu, SWT.SEPARATOR);

		// File->Quit
		item = new MenuItem(menu, SWT.PUSH);
		item.setText("Quit");
		item.setAccelerator(SWT.MOD1 + 'Q');
		item.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				quit();
			}
		});

		return menu;
	}

	/**
	 * Fungsi untuk menyimpan data
	 * 
	 * @return false
	 */
	private boolean saveFile() {
		final FileDialog dialog = new FileDialog(shell, SWT.SAVE);
		dialog.setFilterExtensions(new String[] { "*.html" });
		String fn = dialog.open();
		if (fn != null) {
			File file = new File(fn);
			FileWriter fileWriter;
			try {
				fileWriter = new FileWriter(file);
				fileWriter.write(ManagerBrowser.browser.getText());
				fileWriter.close();
				return true;
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return false;
	}

	/** Method Print File */
	private void printFile() {
		final PrintDialog printDialog = new PrintDialog(shell, SWT.NONE);
		PrinterData printerData = printDialog.open();
		if (!(printerData == null)) {
			Printer p = new Printer(printerData);
			p.startJob("PrintJob");
			p.startPage();
			Rectangle trim = p.computeTrim(0, 0, 0, 0);
			Point dpi = p.getDPI();
			int leftMargin = dpi.x + trim.x;
			int topMargin = dpi.y / 2 + trim.y;
			GC gc = new GC(p);
			Font font = gc.getFont();
			String printText = ManagerBrowser.browser.getText();
			gc.drawString(printText, leftMargin, topMargin
					+ font.getFontData()[0].getHeight());
			p.endPage();
			gc.dispose();
			p.endJob();
			p.dispose();
		}
	}

	/** Method quit */
	private void quit() {
		if (!"".equals(ManagerBrowser.browser.getText())) {
			MessageBox box = new MessageBox(shell, SWT.OK);
			box.setMessage("Apa anda yakin untuk keluar?");
			int result = box.open();
			if (result == SWT.OK) {
				shell.close();
			}
		}
	}

}
